package com.lp.controller;

import com.lp.po.College;
import com.lp.po.PagingVO;
import com.lp.po.Teacher;
import com.lp.service.ArgumentService;
import com.lp.service.CollegeService;
import com.lp.service.TeacherService;
import com.lp.service.UserService;
import com.lp.userException.CustomException;
import com.lp.utils.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/Teacher")
public class AdminTeacherController {
    @Resource
    private CollegeService collegeService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private UserService userService;
    @Resource
    private ArgumentService argumentService;


    /***************************教师管理设置****************************************/
    @RequestMapping("/showTeacher")
    public String showTeacher(Model model, Integer page) throws Exception {
        List<Teacher> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int teacherNumber=teacherService.getCount();
        //设置总页数
        pagingVO.setTotalCount(this.teacherService.getCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = teacherService.findAllTeacher(1);
        } else {
            pagingVO.setToPageNo(page);
            list = teacherService.findAllTeacher(page);
        }
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("teacherList",list);
        model.addAttribute("num",teacherNumber);
        return "/admin/showTeacher";
    }
    // 模糊搜索用户
    @RequestMapping(value = "/selectTeacher", method = {RequestMethod.POST})
    private String selectTeacher(String username, Model model, Integer page) throws Exception {
        PagingVO pagingVO = new PagingVO();
        List<Teacher> list=null;
        int teacherNumberByName=0;
        pagingVO.setTotalCount(this.teacherService.getCountByName('%'+username+'%'));
        if(username!=null){
            if (page == null || page == 0) {
                pagingVO.setToPageNo(1);
                list = teacherService.findByTeacherName(1,'%'+username+'%');
                teacherNumberByName=teacherService.getCountByName('%'+username+'%');
            } else {
                pagingVO.setToPageNo(page);
                list = teacherService.findByTeacherName(page,'%'+username+'%');
                teacherNumberByName=teacherService.getCountByName('%'+username+'%');
            }
            model.addAttribute("pagingVO", pagingVO);
            model.addAttribute("teacherList", list);
            model.addAttribute("num",teacherNumberByName);
            return "/admin/showTeacher";
        }else{
            return "admin/showTeacher";
        }
    }
    //打开addTeacher页面
    @RequestMapping(value = "/addTeacher")
    public String addTeacherUI(Model model) throws Exception {
        List<College> list = collegeService.findAll();
        model.addAttribute("collegeList", list);
        return "/admin/addTeacher";
    }
    // 添加教师信息处理
    @RequestMapping(value = "/doAddTeacher", method = {RequestMethod.POST})
    public String addTeacher(Teacher teacher, Model model, MultipartFile file) throws Exception {
        Boolean result=false;

        if(!file.isEmpty()){
            String uploadUrl = argumentService.getByName("uploadUrl").getValue();
            //图片上传成功后，将图片的地址写到数据库
            teacher.setImg(ImageUtil.getNewFileName(file,uploadUrl));
        }
        //判断教师工号、手机号
        if(teacher.getUserid().toString().length()<11){
            result = teacherService.save(teacher);
        }else {
            throw new CustomException("工号过长，应小于11位");
        }
        if (!result) {
            model.addAttribute("message", "工号重复");
            return "forward:/admin/Teacher/addTeacher";
        }
        //重定向
        return "redirect:/admin/Teacher/showTeacher";
    }
    @RequestMapping(value="editTeacher",method = {RequestMethod.GET})
    public String editTeacher(Integer userid, Model model) throws Exception{
        if (userid == null) {
            return "redirect:/admin/Teacher/showTeacher";
        }
        Teacher teacher = teacherService.findById(userid);
        if (teacher == null) {
            throw new CustomException("未找到该教师");
        }
        List<College> list = collegeService.findAll();
        model.addAttribute("collegeList", list);
        model.addAttribute("teacher", teacher);
        return "/admin/editTeacher";
    }
    @RequestMapping(value="doEditTeacher",method = {RequestMethod.POST})
    public String doEditTeacher(Teacher teacher, MultipartFile file) throws Exception{
        if(!file.isEmpty()) {
            String uploadUrl = argumentService.getByName("uploadUrl").getValue();
            String newFileName= ImageUtil.getNewFileName(file,uploadUrl);
            teacher.setImg(newFileName);
        }
        teacherService.updataById(teacher.getUserid(), teacher);
        return "redirect:/admin/Teacher/showTeacher";
    }
    // 删除用户
    @RequestMapping(value = "/removeTeacher", method = {RequestMethod.GET})
    private String removeTeacher(Integer userid) throws Exception {
        if (userid == null) {
            //加入没有带学生id就进来的话就返回学生显示页面
            return "/admin/showTeacher";
        }
        teacherService.removeById(userid);
        return "redirect:/admin/Teacher/showTeacher";
    }
}
